<template>
	<view class="indexs">
		<view class="header header_lan" :style="'padding-top: ' + navH + 'px;'">
			<view class="header_height" :style="'height:' + gaodu + 'px;line-height:' + gaodu + 'px;top:' + gaodutops + 'px;'">
				<view class="index_h_cont3">
					<view class="index_wenben_img" @tap="jumpzuo"><image :src="imgUrl + '/dzx_img22.png'" mode="widthFix"></image></view>
					<view class="index_wenben baise">{{ $t('page_sharing_user.tzsq') }}</view>
				</view>
			</view>
			<view class="coloe_title">s</view>
		</view>
		<view class="index_center" :style="'padding-top:' + (navH + 50) + 'px;padding-bottom: 120rpx;'">
			<view class="baoguo_center">
				<view class="baoguo_item2">
					<view class="baoguo_item2_cont">
						<view class="baoguo_item2_cont_l">
							<image :src="imgUrl + '/dzx_img29.png'"></image>
							<text>{{ $t('page_sharing_user.Real_name') }}</text>
						</view>
						<view class="baoguo_item2_cont_r"><input type="text" data-field="truename" @input="bindInput" :placeholder="$t('page_sharing_user.p_Real_name')" /></view>
					</view>
					<view class="baoguo_item2_cont">
						<view class="baoguo_item2_cont_l">
							<image :src="imgUrl + '/dzx_img29.png'"></image>
							<text>{{ $t('page_sharing_user.idcard') }}</text>
						</view>
						<view class="baoguo_item2_cont_r"><input type="text" data-field="idcard" @input="bindInput" :placeholder="$t('page_sharing_user.p_idcard')" /></view>
					</view>
					<view class="baoguo_item2_cont">
						<view class="baoguo_item2_cont_l">
							<image :src="imgUrl + '/dzx_img29.png'"></image>
							<text>{{ $t('page_sharing_user.phone') }}</text>
						</view>
						<view class="baoguo_item2_cont_r">
							<input type="text" data-field="mobile" :value="form.mobile" @input="bindInput" :placeholder="$t('page_sharing_user.placeholder')" />
							<button class="phoneclass" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">{{ $t('page_sharing_user.get') }}</button>
						</view>
					</view>
					<view class="baoguo_btns">
						<view class="baoguo_btns_posi">
							<view class="baoguo_btn" @tap="sumbit">{{ $t('page_sharing_user.Submit_pplication') }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
// pages/shouye/shouye_zkc/shouye_zkc.js
const app = getApp();

let request = require('../../utils/require');

export default {
	data() {
		return {
			navH: app.globalData.navHeight,
			gaodu: app.globalData.height,
			gaodutops: app.globalData.top,
			imgUrl: app.globalData.imgUrl,
			form: {
				truename: '',
				idcard: '',
				mobile: ''
			},
			mcode: ''
		};
	},
	/**
	 * 生命周期函数--监听页面加载
	 */ onLoad() {
		var that = this;
	},
	onShow() {},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {},
	methods: {
		jumpzuo() {
			uni.navigateBack();
		},
		bindInput(e) {
			var form = this.form;
			var field = e.currentTarget.dataset.field;
			form[field] = e.detail.value;
			this.form = form;
		},
		getPhoneNumber(e) {
			var that = this;
			var iv = e.detail.iv;
			var encryptedData = e.detail.encryptedData;
			var form = that.form;
			uni.login({
				success(res) {
					if (res.code) {
						var data = {
							token: uni.getStorageSync('token'),
							iv: encodeURIComponent(iv),
							encryptedData: encodeURIComponent(encryptedData),
							code: res.code
						};

						if (e.detail.errMsg == 'getPhoneNumber:ok') {
							request.post(`index.php?s=/api/sharp.index/memberbindmobile&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
								if (res.data.code == 1) {
									wx.showToast({
										title: '获取成功'
									});
									form['mobile'] = res.data.data;
									that.form= form
								} else {
									wx.showToast({
										title: res.data.message,
										icon: 'none'
									});
								}
							});
						}
					}
				}
			});
		},
		sumbit() {
			var form = this.form;
			if (!this.sumbitCheck(form)) {
				uni.showToast({
					icon: 'none',
					title: '请填写完必填资料'
				});
				return;
			}
			if (!this.sumbitCheckMobile(form)) {
				uni.showToast({
					icon: 'none',
					title: '请输入正确的手机号'
				});
				return;
			}
			var that = this;
			uni.showLoading({
				title: '提交资料中'
			});
			form['token'] = uni.getStorageSync('token');
			request.post(`index.php?s=/api/sharp.index/apply&wxapp_id=` + uni.getStorageSync('web_id'), form).then(res => {
				uni.hideLoading();
				if (res.data.code == 1) {
					uni.showToast({
						title: res.data.data,
						duration: 2000,
						success: function() {
							setTimeout(function() {
								uni.navigateBack({
									delta: 1
								});
							}, 1500);
						}
					});
				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.data
					});
				}
			});
		},
		sumbitCheck(form) {
			for (var _k in form) {
				if (form[_k] == '') {
					return false;
				}
			}
			return true;
		},

		sumbitCheckMobile(form) {
			if (!/^1[3456789]\d{9}$/.test(form['mobile'])) {
				return false;
			}
			return true;
		}
	}
};
</script>

<style>
@import './user.css';
</style>
